#include "Button.h"
#include "ResourceManager.h"

Button::Button(char* _buttonName, float _posX, float _posY, float _posZ, float _sizeW,  float _sizeH, float _colorR, float _colorG, float _colorB, float _colorA)
{
	m_buttonName = _buttonName;
	m_pos.X = _posX;
	m_pos.Y = _posY;
	m_pos.Z = _posZ;
	
	m_size.X = _sizeW;
	m_size.Y = _sizeH;
	
	m_color.R = _colorR;
	m_color.G = _colorG;
	m_color.B = _colorB;
	m_color.A = _colorA;
	m_isActive = true;
}


Button::~Button(void)
{
}
void Button::DrawButton(float _anpha)
{
	if(!m_isActive)
		return;
	glPushMatrix();

	glTranslatef(m_pos.X, m_pos.Y, m_pos.Z);

	//glColor4f(m_color.X, m_color.Y, m_color.Z, m_color.T);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1, 1, 1, _anpha);
	ResourceManager::GetInst()->LoadTexture(m_buttonName);
	float w,h;
	w = m_size.X/2;
	h = m_size.Y/2;
	// draw the triangle such that the rotation point is in the center
	glBegin(GL_QUADS);
	
	glTexCoord2f(1, 0); glVertex3f(w, -h, 0);
	glTexCoord2f(0, 0); glVertex3f(-w, -h, 0);
	glTexCoord2f(0, 1); glVertex3f(-w, h, 0);
	glTexCoord2f(1, 1); glVertex3f(w, h, 0);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glEnd();
	glPopMatrix();
}
void Button::DrawButton()
{
	if(!m_isActive)
		return;
	glPushMatrix();

	glTranslatef(m_pos.X, m_pos.Y, m_pos.Z);

	//glColor4f(m_color.X, m_color.Y, m_color.Z, m_color.T);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1, 1, 1, 1);
	ResourceManager::GetInst()->LoadTexture(m_buttonName);
	float w,h;
	w = m_size.X/2;
	h = m_size.Y/2;
	// draw the triangle such that the rotation point is in the center
	glBegin(GL_QUADS);

	glTexCoord2f(1, 0); glVertex3f(w, -h, 0);
	glTexCoord2f(0, 0); glVertex3f(-w, -h, 0);
	glTexCoord2f(0, 1); glVertex3f(-w, h, 0);
	glTexCoord2f(1, 1); glVertex3f(w, h, 0);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glEnd();
	glPopMatrix();
}
bool Button::IsMouseMoving(float _mouseX, float _mouseY)
{
	if(!m_isActive)
		return false;
	if(_mouseX < (m_pos.X - m_size.X/2) || _mouseX > (m_pos.X + m_size.X/2))
		return false;
	if(_mouseY < (m_pos.Y - m_size.Y/2) || _mouseY > (m_pos.Y + m_size.Y/2))
		return false;
	return true;
}
void Button::SetButtonPos(float _posX, float _posY, float _posZ)
{
	m_pos.X = _posX;
	m_pos.Y = _posY;
	m_pos.Z = _posZ;
}